// Administrator Controller

function adminLoginCtrl($scope, $rootScope, $location, $http) {

    _log('### Administrator Login page ###');
    // still admin login page
    $rootScope.adminLoginPage = true;
    // login user
    $scope.account = {};
    $scope.loginUser = function (account) {

        _log('### login function ###');
        if (_COMMON.validated()) {

            // splash screen loading
            _COMMON.loading(500);

            var config = {
                params: {
                    wsname: 'authen',
                    token: _pageData.tokenKey,
                    username: $scope.account.username,
                    password: $scope.account.password
                }
            };

            $http.post(_COMMON.context + 'login_ws.php', null, config)
                .success(function (obj) {
                    _log(obj);
                    if (obj.authentication.isauthen) {

                        _log('### Login successful ###');

                        _pageData.isAuthen = obj.authentication.isauthen;
                        _pageData.user = obj.authentication.user;
                        _COMMON.setPageData(_pageData);

                        //FIXME: first time  css is incorrect for admin dashboard
                        $location.path('/admin/dashboard'); //redirect to admin dashboard.

                    } else {
                        //('### Login failed ###');
                        //alert('Username or Password is incorrect !');
                        notif({
                            msg: "<b><i class='fa fa-thumbs-o-down'></i>  Username or Password Incorrect !</b>",
                            type: "error",
                            position: "center"
                        });
                        
                        $scope.account.username = '';
                        $scope.account.password = '';
                    }
                })
                .error(function () {
                    alert('Error is occured, May be database connect is wrong');
                });

        } else {
            // validate not pass.
            _log('### validate not pass ###');
        }
    };

    //  --- Set Middle Loginform 
    var midW = window.innerWidth / 2;
    var midH = window.innerHeight / 2;
    $("#login").css("top", midH - parseInt($("#login").height() / 2));
    $("#login").css("left", midW - parseInt($("#login").width() / 2));

    window.onresize = function () {
        midW = window.innerWidth / 2;
        midH = window.innerHeight / 2;      
        $("#login").css("top", midH - parseInt($("#login").height() / 2));
        $("#login").css("left", midW - parseInt($("#login").width() / 2));
    };


    // ---- If Has User In Session
    if (_COMMON.getPageData()) {
        _pageData = _COMMON.getPageData();
        $rootScope.pageData = _pageData;
        $location.path('/admin/dashboard');

        notif({
            msg: "<b><i class='fa fa-warning'></i> You Already Login !</b>",
            type: "info",
            position: "center"
        });

    } else {
        window.onresize();
    }

}
